//+------------------------------------------------------------------+
//| 五浪理论增强日志模块
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include "WaveTheory_GA_Logger.mqh"

// 波浪结构信息
struct WaveStructure {
    int startBar;           // 起始K线
    int endBar;             // 结束K线
    double startPrice;      // 起始价格
    double endPrice;        // 结束价格
    double high;            // 最高价
    double low;             // 最低价
    double amplitude;       // 幅度
    double retracement;     // 回撤比例
    double extension;       // 延伸比例
    double duration;        // 持续时间
    double volume;          // 成交量
    double strength;        // 强度
    string pattern;         // 形态
    bool isValid;           // 是否有效
    string reason;          // 原因
};

// 全局波浪结构数组
WaveStructure g_waveStructures[];

//+------------------------------------------------------------------+
//| 记录完整的五浪结构分析
//+------------------------------------------------------------------+
void LogCompleteWaveAnalysis(int currentBar, const double &high[], const double &low[], 
                            const double &close[], const double &volume[]) {
    if(!g_logConfig.enableWaveLog) return;
    
    LogMessage(LOG_INFO, "=== 五浪结构完整分析 ===");
    LogMessage(LOG_INFO, "分析时间: " + TimeToString(Time(currentBar), TIME_DATE | TIME_SECONDS));
    LogMessage(LOG_INFO, "当前K线: " + IntegerToString(currentBar));
    LogMessage(LOG_INFO, "当前价格: " + DoubleToString(close[currentBar], _Digits));
    
    // 分析最近的K线数据
    int lookback = MathMin(100, currentBar);
    for(int i = currentBar - lookback; i <= currentBar; i++) {
        if(i < 0) continue;
        
        // 记录每个K线的基本信息
        string klineInfo = StringFormat("K线[%d] - 时间:%s 开盘:%.5f 最高:%.5f 最低:%.5f 收盘:%.5f 成交量:%.0f",
                                       i, TimeToString(Time(i), TIME_DATE | TIME_SECONDS),
                                       i < currentBar ? close[i+1] : close[i], // 开盘价
                                       high[i], low[i], close[i], volume[i]);
        LogMessage(LOG_DEBUG, klineInfo);
    }
    
    // 寻找潜在的波浪结构
    FindPotentialWaves(currentBar, high, low, close, volume);
}

//+------------------------------------------------------------------+
//| 寻找潜在的波浪结构
//+------------------------------------------------------------------+
void FindPotentialWaves(int currentBar, const double &high[], const double &low[], 
                       const double &close[], const double &volume[]) {
    LogMessage(LOG_INFO, "=== 寻找潜在波浪结构 ===");
    
    // 寻找局部极值点
    int extremumPoints[];
    FindExtremumPoints(currentBar, high, low, extremumPoints);
    
    LogMessage(LOG_INFO, "找到极值点数量: " + IntegerToString(ArraySize(extremumPoints)));
    
    // 分析每个极值点
    for(int i = 0; i < ArraySize(extremumPoints); i++) {
        int pointIndex = extremumPoints[i];
        LogMessage(LOG_INFO, StringFormat("极值点[%d] - 位置:%d 价格:%.5f 时间:%s",
                                         i, pointIndex, close[pointIndex], 
                                         TimeToString(Time(pointIndex), TIME_DATE | TIME_SECONDS)));
    }
    
    // 尝试识别五浪结构
    IdentifyWavePattern(currentBar, extremumPoints, high, low, close, volume);
}

//+------------------------------------------------------------------+
//| 寻找极值点
//+------------------------------------------------------------------+
void FindExtremumPoints(int currentBar, const double &high[], const double &low[], 
                       int &extremumPoints[]) {
    ArrayResize(extremumPoints, 0);
    
    // 在最近的50根K线中寻找极值点
    int lookback = MathMin(50, currentBar);
    for(int i = 2; i < lookback - 2; i++) {
        int barIndex = currentBar - lookback + i;
        
        // 寻找局部高点
        if(high[barIndex] > high[barIndex-1] && high[barIndex] > high[barIndex-2] &&
           high[barIndex] > high[barIndex+1] && high[barIndex] > high[barIndex+2]) {
            ArrayResize(extremumPoints, ArraySize(extremumPoints) + 1);
            extremumPoints[ArraySize(extremumPoints) - 1] = barIndex;
            
            LogMessage(LOG_DEBUG, StringFormat("发现局部高点 - 位置:%d 价格:%.5f",
                                             barIndex, high[barIndex]));
        }
        
        // 寻找局部低点
        if(low[barIndex] < low[barIndex-1] && low[barIndex] < low[barIndex-2] &&
           low[barIndex] < low[barIndex+1] && low[barIndex] < low[barIndex+2]) {
            ArrayResize(extremumPoints, ArraySize(extremumPoints) + 1);
            extremumPoints[ArraySize(extremumPoints) - 1] = barIndex;
            
            LogMessage(LOG_DEBUG, StringFormat("发现局部低点 - 位置:%d 价格:%.5f",
                                             barIndex, low[barIndex]));
        }
    }
}

//+------------------------------------------------------------------+
//| 识别波浪形态
//+------------------------------------------------------------------+
void IdentifyWavePattern(int currentBar, const int &extremumPoints[], 
                        const double &high[], const double &low[], 
                        const double &close[], const double &volume[]) {
    LogMessage(LOG_INFO, "=== 波浪形态识别 ===");
    
    if(ArraySize(extremumPoints) < 5) {
        LogMessage(LOG_WARNING, "极值点数量不足，无法识别完整的五浪结构");
        return;
    }
    
    // 尝试识别第1浪
    for(int i = 0; i < ArraySize(extremumPoints) - 4; i++) {
        int wave1Start = extremumPoints[i];
        int wave1End = extremumPoints[i + 1];
        
        WaveStructure wave1;
        wave1.startBar = wave1Start;
        wave1.endBar = wave1End;
        wave1.startPrice = close[wave1Start];
        wave1.endPrice = close[wave1End];
        wave1.high = high[wave1End];
        wave1.low = low[wave1Start];
        wave1.amplitude = wave1.high - wave1.low;
        wave1.duration = (double)(wave1End - wave1Start);
        wave1.volume = CalculateAverageVolume(wave1Start, wave1End, volume);
        wave1.strength = CalculateWaveStrength(wave1Start, wave1End, high, low, close);
        
        // 判断是否为有效的第1浪
        bool isValidWave1 = ValidateWave1(wave1);
        wave1.isValid = isValidWave1;
        wave1.pattern = "第1浪";
        
        LogWaveStructure(wave1);
        
        if(isValidWave1) {
            // 继续识别后续波浪
            IdentifyRemainingWaves(currentBar, i + 1, extremumPoints, high, low, close, volume, wave1);
        }
    }
}

//+------------------------------------------------------------------+
//| 识别剩余波浪
//+------------------------------------------------------------------+
void IdentifyRemainingWaves(int currentBar, int startIndex, const int &extremumPoints[], 
                           const double &high[], const double &low[], 
                           const double &close[], const double &volume[], 
                           WaveStructure &wave1) {
    LogMessage(LOG_INFO, "=== 识别剩余波浪 ===");
    
    if(startIndex + 3 >= ArraySize(extremumPoints)) {
        LogMessage(LOG_WARNING, "极值点不足，无法识别完整的五浪结构");
        return;
    }
    
    // 第2浪
    int wave2Start = extremumPoints[startIndex];
    int wave2End = extremumPoints[startIndex + 1];
    
    WaveStructure wave2;
    wave2.startBar = wave2Start;
    wave2.endBar = wave2End;
    wave2.startPrice = wave1.endPrice;
    wave2.endPrice = close[wave2End];
    wave2.high = high[wave2Start];
    wave2.low = low[wave2End];
    wave2.amplitude = wave2.high - wave2.low;
    wave2.retracement = (wave1.high - wave2.low) / wave1.amplitude;
    wave2.duration = (double)(wave2End - wave2Start);
    wave2.volume = CalculateAverageVolume(wave2Start, wave2End, volume);
    wave2.strength = CalculateWaveStrength(wave2Start, wave2End, high, low, close);
    wave2.isValid = ValidateWave2(wave2, wave1);
    wave2.pattern = "第2浪";
    
    LogWaveStructure(wave2);
    
    if(!wave2.isValid) return;
    
    // 第3浪
    int wave3Start = wave2End;
    int wave3End = extremumPoints[startIndex + 2];
    
    WaveStructure wave3;
    wave3.startBar = wave3Start;
    wave3.endBar = wave3End;
    wave3.startPrice = wave2.endPrice;
    wave3.endPrice = close[wave3End];
    wave3.high = high[wave3End];
    wave3.low = low[wave3Start];
    wave3.amplitude = wave3.high - wave3.low;
    wave3.extension = wave3.amplitude / wave1.amplitude;
    wave3.duration = (double)(wave3End - wave3Start);
    wave3.volume = CalculateAverageVolume(wave3Start, wave3End, volume);
    wave3.strength = CalculateWaveStrength(wave3Start, wave3End, high, low, close);
    wave3.isValid = ValidateWave3(wave3, wave1, wave2);
    wave3.pattern = "第3浪";
    
    LogWaveStructure(wave3);
    
    if(!wave3.isValid) return;
    
    // 第4浪
    int wave4Start = wave3End;
    int wave4End = extremumPoints[startIndex + 3];
    
    WaveStructure wave4;
    wave4.startBar = wave4Start;
    wave4.endBar = wave4End;
    wave4.startPrice = wave3.endPrice;
    wave4.endPrice = close[wave4End];
    wave4.high = high[wave4Start];
    wave4.low = low[wave4End];
    wave4.amplitude = wave4.high - wave4.low;
    wave4.retracement = (wave3.high - wave4.low) / wave3.amplitude;
    wave4.duration = (double)(wave4End - wave4Start);
    wave4.volume = CalculateAverageVolume(wave4Start, wave4End, volume);
    wave4.strength = CalculateWaveStrength(wave4Start, wave4End, high, low, close);
    wave4.isValid = ValidateWave4(wave4, wave1, wave2, wave3);
    wave4.pattern = "第4浪";
    
    LogWaveStructure(wave4);
    
    if(!wave4.isValid) return;
    
    // 第5浪
    int wave5Start = wave4End;
    int wave5End = currentBar;
    
    WaveStructure wave5;
    wave5.startBar = wave5Start;
    wave5.endBar = wave5End;
    wave5.startPrice = wave4.endPrice;
    wave5.endPrice = close[wave5End];
    wave5.high = high[wave5End];
    wave5.low = low[wave5Start];
    wave5.amplitude = wave5.high - wave5.low;
    wave5.extension = wave5.amplitude / wave1.amplitude;
    wave5.duration = (double)(wave5End - wave5Start);
    wave5.volume = CalculateAverageVolume(wave5Start, wave5End, volume);
    wave5.strength = CalculateWaveStrength(wave5Start, wave5End, high, low, close);
    wave5.isValid = ValidateWave5(wave5, wave1, wave2, wave3, wave4);
    wave5.pattern = "第5浪";
    
    LogWaveStructure(wave5);
    
    // 生成完整的五浪分析报告
    if(wave5.isValid) {
        GenerateWaveAnalysisReport(wave1, wave2, wave3, wave4, wave5);
    }
}

//+------------------------------------------------------------------+
//| 记录波浪结构信息
//+------------------------------------------------------------------+
void LogWaveStructure(WaveStructure &wave) {
    string message = StringFormat("波浪结构 - %s 起始:%d 结束:%d 起始价:%.5f 结束价:%.5f 幅度:%.5f 强度:%.3f 有效:%s",
                                 wave.pattern, wave.startBar, wave.endBar, wave.startPrice, 
                                 wave.endPrice, wave.amplitude, wave.strength, 
                                 wave.isValid ? "是" : "否");
    LogMessage(LOG_INFO, message);
    
    if(wave.retracement > 0) {
        LogMessage(LOG_DEBUG, StringFormat("  - 回撤比例: %.1f%%", wave.retracement * 100));
    }
    if(wave.extension > 0) {
        LogMessage(LOG_DEBUG, StringFormat("  - 延伸比例: %.2f", wave.extension));
    }
    LogMessage(LOG_DEBUG, StringFormat("  - 持续时间: %.0f根K线", wave.duration));
    LogMessage(LOG_DEBUG, StringFormat("  - 平均成交量: %.0f", wave.volume));
    
    if(!wave.isValid && wave.reason != "") {
        LogMessage(LOG_WARNING, "  - 无效原因: " + wave.reason);
    }
}

//+------------------------------------------------------------------+
//| 生成波浪分析报告
//+------------------------------------------------------------------+
void GenerateWaveAnalysisReport(WaveStructure &wave1, WaveStructure &wave2, 
                               WaveStructure &wave3, WaveStructure &wave4, 
                               WaveStructure &wave5) {
    LogMessage(LOG_INFO, "=== 五浪结构完整分析报告 ===");
    
    // 基本统计
    double totalAmplitude = wave1.amplitude + wave2.amplitude + wave3.amplitude + 
                           wave4.amplitude + wave5.amplitude;
    double totalDuration = wave1.duration + wave2.duration + wave3.duration + 
                          wave4.duration + wave5.duration;
    double totalVolume = wave1.volume + wave2.volume + wave3.volume + 
                        wave4.volume + wave5.volume;
    
    LogMessage(LOG_INFO, StringFormat("总幅度: %.5f 总持续时间: %.0f根K线 总成交量: %.0f", 
                                     totalAmplitude, totalDuration, totalVolume));
    
    // 各浪特征分析
    LogMessage(LOG_INFO, "=== 各浪特征分析 ===");
    
    // 第1浪特征
    LogMessage(LOG_INFO, StringFormat("第1浪 - 幅度:%.5f(%.1f%%) 强度:%.3f 成交量:%.0f", 
                                     wave1.amplitude, wave1.amplitude/totalAmplitude*100, 
                                     wave1.strength, wave1.volume));
    
    // 第2浪特征
    LogMessage(LOG_INFO, StringFormat("第2浪 - 回撤:%.1f%% 幅度:%.5f(%.1f%%) 强度:%.3f", 
                                     wave2.retracement*100, wave2.amplitude, 
                                     wave2.amplitude/totalAmplitude*100, wave2.strength));
    
    // 第3浪特征
    LogMessage(LOG_INFO, StringFormat("第3浪 - 延伸:%.2f 幅度:%.5f(%.1f%%) 强度:%.3f 成交量:%.0f", 
                                     wave3.extension, wave3.amplitude, 
                                     wave3.amplitude/totalAmplitude*100, wave3.strength, wave3.volume));
    
    // 第4浪特征
    LogMessage(LOG_INFO, StringFormat("第4浪 - 回撤:%.1f%% 幅度:%.5f(%.1f%%) 强度:%.3f", 
                                     wave4.retracement*100, wave4.amplitude, 
                                     wave4.amplitude/totalAmplitude*100, wave4.strength));
    
    // 第5浪特征
    LogMessage(LOG_INFO, StringFormat("第5浪 - 延伸:%.2f 幅度:%.5f(%.1f%%) 强度:%.3f", 
                                     wave5.extension, wave5.amplitude, 
                                     wave5.amplitude/totalAmplitude*100, wave5.strength));
    
    // 波浪关系分析
    LogMessage(LOG_INFO, "=== 波浪关系分析 ===");
    
    // 第3浪是否最长
    bool wave3Longest = (wave3.amplitude > wave1.amplitude) && (wave3.amplitude > wave5.amplitude);
    LogMessage(LOG_INFO, StringFormat("第3浪是否最长: %s", wave3Longest ? "是" : "否"));
    
    // 第2浪和第4浪交替
    bool wave2Wave4Alternate = (wave2.retracement > 0.5 && wave4.retracement < 0.5) ||
                               (wave2.retracement < 0.5 && wave4.retracement > 0.5);
    LogMessage(LOG_INFO, StringFormat("第2浪和第4浪是否交替: %s", wave2Wave4Alternate ? "是" : "否"));
    
    // 斐波那契关系
    AnalyzeFibonacciRelations(wave1, wave2, wave3, wave4, wave5);
    
    // 成交量分析
    AnalyzeVolumePattern(wave1, wave2, wave3, wave4, wave5);
    
    // 趋势强度分析
    AnalyzeTrendStrength(wave1, wave2, wave3, wave4, wave5);
}

//+------------------------------------------------------------------+
//| 分析斐波那契关系
//+------------------------------------------------------------------+
void AnalyzeFibonacciRelations(WaveStructure &wave1, WaveStructure &wave2, 
                              WaveStructure &wave3, WaveStructure &wave4, 
                              WaveStructure &wave5) {
    LogMessage(LOG_INFO, "=== 斐波那契关系分析 ===");
    
    // 第2浪回撤的斐波那契关系
    double fib236 = 0.236, fib382 = 0.382, fib500 = 0.500, fib618 = 0.618;
    
    LogMessage(LOG_INFO, StringFormat("第2浪回撤: %.1f%%", wave2.retracement * 100));
    LogMessage(LOG_INFO, StringFormat("  接近23.6%%: %s", MathAbs(wave2.retracement - fib236) < 0.05 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近38.2%%: %s", MathAbs(wave2.retracement - fib382) < 0.05 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近50.0%%: %s", MathAbs(wave2.retracement - fib500) < 0.05 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近61.8%%: %s", MathAbs(wave2.retracement - fib618) < 0.05 ? "是" : "否"));
    
    // 第4浪回撤的斐波那契关系
    LogMessage(LOG_INFO, StringFormat("第4浪回撤: %.1f%%", wave4.retracement * 100));
    LogMessage(LOG_INFO, StringFormat("  接近23.6%%: %s", MathAbs(wave4.retracement - fib236) < 0.05 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近38.2%%: %s", MathAbs(wave4.retracement - fib382) < 0.05 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近50.0%%: %s", MathAbs(wave4.retracement - fib500) < 0.05 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近61.8%%: %s", MathAbs(wave4.retracement - fib618) < 0.05 ? "是" : "否"));
    
    // 第3浪延伸的斐波那契关系
    LogMessage(LOG_INFO, StringFormat("第3浪延伸: %.2f", wave3.extension));
    LogMessage(LOG_INFO, StringFormat("  接近1.000: %s", MathAbs(wave3.extension - 1.0) < 0.1 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近1.618: %s", MathAbs(wave3.extension - 1.618) < 0.1 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近2.000: %s", MathAbs(wave3.extension - 2.0) < 0.1 ? "是" : "否"));
    LogMessage(LOG_INFO, StringFormat("  接近2.618: %s", MathAbs(wave3.extension - 2.618) < 0.1 ? "是" : "否"));
}

//+------------------------------------------------------------------+
//| 分析成交量模式
//+------------------------------------------------------------------+
void AnalyzeVolumePattern(WaveStructure &wave1, WaveStructure &wave2, 
                         WaveStructure &wave3, WaveStructure &wave4, 
                         WaveStructure &wave5) {
    LogMessage(LOG_INFO, "=== 成交量模式分析 ===");
    
    double avgVolume = (wave1.volume + wave2.volume + wave3.volume + wave4.volume + wave5.volume) / 5.0;
    
    LogMessage(LOG_INFO, StringFormat("平均成交量: %.0f", avgVolume));
    LogMessage(LOG_INFO, StringFormat("第1浪成交量: %.0f (%.1f%%)", wave1.volume, wave1.volume/avgVolume*100));
    LogMessage(LOG_INFO, StringFormat("第2浪成交量: %.0f (%.1f%%)", wave2.volume, wave2.volume/avgVolume*100));
    LogMessage(LOG_INFO, StringFormat("第3浪成交量: %.0f (%.1f%%)", wave3.volume, wave3.volume/avgVolume*100));
    LogMessage(LOG_INFO, StringFormat("第4浪成交量: %.0f (%.1f%%)", wave4.volume, wave4.volume/avgVolume*100));
    LogMessage(LOG_INFO, StringFormat("第5浪成交量: %.0f (%.1f%%)", wave5.volume, wave5.volume/avgVolume*100));
    
    // 成交量趋势分析
    bool volumeIncreasing = (wave3.volume > wave1.volume) && (wave5.volume > wave3.volume);
    LogMessage(LOG_INFO, StringFormat("成交量是否递增: %s", volumeIncreasing ? "是" : "否"));
    
    // 第3浪成交量是否最大
    bool wave3VolumeHighest = (wave3.volume > wave1.volume) && (wave3.volume > wave2.volume) &&
                              (wave3.volume > wave4.volume) && (wave3.volume > wave5.volume);
    LogMessage(LOG_INFO, StringFormat("第3浪成交量是否最大: %s", wave3VolumeHighest ? "是" : "否"));
}

//+------------------------------------------------------------------+
//| 分析趋势强度
//+------------------------------------------------------------------+
void AnalyzeTrendStrength(WaveStructure &wave1, WaveStructure &wave2, 
                         WaveStructure &wave3, WaveStructure &wave4, 
                         WaveStructure &wave5) {
    LogMessage(LOG_INFO, "=== 趋势强度分析 ===");
    
    double avgStrength = (wave1.strength + wave2.strength + wave3.strength + wave4.strength + wave5.strength) / 5.0;
    
    LogMessage(LOG_INFO, StringFormat("平均强度: %.3f", avgStrength));
    LogMessage(LOG_INFO, StringFormat("第1浪强度: %.3f", wave1.strength));
    LogMessage(LOG_INFO, StringFormat("第2浪强度: %.3f", wave2.strength));
    LogMessage(LOG_INFO, StringFormat("第3浪强度: %.3f", wave3.strength));
    LogMessage(LOG_INFO, StringFormat("第4浪强度: %.3f", wave4.strength));
    LogMessage(LOG_INFO, StringFormat("第5浪强度: %.3f", wave5.strength));
    
    // 趋势强度评估
    string trendStrength = "弱";
    if(avgStrength > 0.7) trendStrength = "强";
    else if(avgStrength > 0.4) trendStrength = "中等";
    
    LogMessage(LOG_INFO, StringFormat("整体趋势强度: %s", trendStrength));
    
    // 各浪强度比较
    LogMessage(LOG_INFO, StringFormat("最强波浪: %s", GetStrongestWave(wave1, wave2, wave3, wave4, wave5)));
    LogMessage(LOG_INFO, StringFormat("最弱波浪: %s", GetWeakestWave(wave1, wave2, wave3, wave4, wave5)));
}

//+------------------------------------------------------------------+
//| 辅助函数
//+------------------------------------------------------------------+

// 计算平均成交量
double CalculateAverageVolume(int startBar, int endBar, const double &volume[]) {
    double totalVolume = 0;
    int count = 0;
    
    for(int i = startBar; i <= endBar && i < ArraySize(volume); i++) {
        totalVolume += volume[i];
        count++;
    }
    
    return (count > 0) ? (totalVolume / count) : 0;
}

// 计算波浪强度
double CalculateWaveStrength(int startBar, int endBar, const double &high[], 
                           const double &low[], const double &close[]) {
    if(endBar <= startBar) return 0;
    
    double amplitude = 0;
    double totalMove = 0;
    
    for(int i = startBar; i <= endBar && i < ArraySize(close); i++) {
        if(i > startBar) {
            double move = MathAbs(close[i] - close[i-1]);
            totalMove += move;
        }
        
        if(high[i] - low[i] > amplitude) {
            amplitude = high[i] - low[i];
        }
    }
    
    double avgMove = (endBar - startBar > 0) ? (totalMove / (endBar - startBar)) : 0;
    return (amplitude > 0) ? (avgMove / amplitude) : 0;
}

// 验证第1浪
bool ValidateWave1(WaveStructure &wave) {
    // 第1浪应该有一定的幅度
    if(wave.amplitude < 0.0001) {
        wave.reason = "幅度太小";
        return false;
    }
    
    // 第1浪应该有合理的持续时间
    if(wave.duration < 3) {
        wave.reason = "持续时间太短";
        return false;
    }
    
    return true;
}

// 验证第2浪
bool ValidateWave2(WaveStructure &wave, WaveStructure &wave1) {
    // 第2浪回撤应该在合理范围内
    if(wave.retracement < 0.236 || wave.retracement > 0.786) {
        wave.reason = "回撤比例超出范围";
        return false;
    }
    
    // 第2浪不应该超过第1浪的起点
    if(wave.low < wave1.low) {
        wave.reason = "回撤超过第1浪起点";
        return false;
    }
    
    return true;
}

// 验证第3浪
bool ValidateWave3(WaveStructure &wave, WaveStructure &wave1, WaveStructure &wave2) {
    // 第3浪应该超过第1浪的高点
    if(wave.high <= wave1.high) {
        wave.reason = "未超过第1浪高点";
        return false;
    }
    
    // 第3浪应该有合理的延伸
    if(wave.extension < 1.0) {
        wave.reason = "延伸不足";
        return false;
    }
    
    return true;
}

// 验证第4浪
bool ValidateWave4(WaveStructure &wave, WaveStructure &wave1, WaveStructure &wave2, WaveStructure &wave3) {
    // 第4浪回撤应该在合理范围内
    if(wave.retracement < 0.236 || wave.retracement > 0.786) {
        wave.reason = "回撤比例超出范围";
        return false;
    }
    
    // 第4浪不应该进入第1浪的区域
    if(wave.low < wave1.high) {
        wave.reason = "回撤进入第1浪区域";
        return false;
    }
    
    return true;
}

// 验证第5浪
bool ValidateWave5(WaveStructure &wave, WaveStructure &wave1, WaveStructure &wave2, 
                   WaveStructure &wave3, WaveStructure &wave4) {
    // 第5浪应该超过第3浪的高点
    if(wave.high <= wave3.high) {
        wave.reason = "未超过第3浪高点";
        return false;
    }
    
    // 第5浪应该有合理的延伸
    if(wave.extension < 0.5) {
        wave.reason = "延伸不足";
        return false;
    }
    
    return true;
}

// 获取最强波浪
string GetStrongestWave(WaveStructure &wave1, WaveStructure &wave2, 
                       WaveStructure &wave3, WaveStructure &wave4, 
                       WaveStructure &wave5) {
    double maxStrength = MathMax(MathMax(MathMax(MathMax(wave1.strength, wave2.strength), 
                                                 wave3.strength), wave4.strength), wave5.strength);
    
    if(maxStrength == wave1.strength) return "第1浪";
    if(maxStrength == wave2.strength) return "第2浪";
    if(maxStrength == wave3.strength) return "第3浪";
    if(maxStrength == wave4.strength) return "第4浪";
    return "第5浪";
}

// 获取最弱波浪
string GetWeakestWave(WaveStructure &wave1, WaveStructure &wave2, 
                     WaveStructure &wave3, WaveStructure &wave4, 
                     WaveStructure &wave5) {
    double minStrength = MathMin(MathMin(MathMin(MathMin(wave1.strength, wave2.strength), 
                                                 wave3.strength), wave4.strength), wave5.strength);
    
    if(minStrength == wave1.strength) return "第1浪";
    if(minStrength == wave2.strength) return "第2浪";
    if(minStrength == wave3.strength) return "第3浪";
    if(minStrength == wave4.strength) return "第4浪";
    return "第5浪";
} 